package com.guigu.qingcheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guigu.qingcheng.mapper.TbSkuMapper;
import com.guigu.qingcheng.pojo.TbOrderItem;
import com.guigu.qingcheng.pojo.TbSku;
import com.guigu.qingcheng.pojo.vo.SpuAndSku;
import com.guigu.qingcheng.service.TbSkuService;
import com.guigu.qingcheng.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
*
* @author Jiang
* @date 2020/07/25
*/
@Service
public class TbSkuServiceImpl implements TbSkuService {
    @Autowired(required = false)
    private TbSkuMapper tbSkuMapper;

    @Override
    public Map findsku3() {
        return tbSkuMapper.findsku3();
    }
    @Override
    public Map findsku2() {
        return tbSkuMapper.findsku2();
    }
    @Override
    public Map findsku() {
        return tbSkuMapper.findsku();
    }

    @Override
    public List<TbSku> queryAll() {
        List<TbSku> tbSku = tbSkuMapper.selectAll();
            return tbSku;
    }

    @Override
    public PageUtil<TbSku> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbSku> pages = (Page<TbSku>) tbSkuMapper.selectAll();
        PageUtil<TbSku> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbSku> pageQueryByAll(Integer pageNum, Integer pageSize, TbSku tbSku) {

        Example example=new Example(TbSku.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO 待实现 重新实现判断参数

        //if(!StringUtils.isEmpty(brand.getId())){
        //    criteria.andEqualTo("id",brand.getId());
        //}
        //if(!StringUtils.isEmpty(brand.getName())){
        //   criteria.andLike("name",brand.getName());
        //}
        PageHelper.startPage(pageNum,pageSize);
        Page<TbSku> pages = (Page<TbSku>) tbSkuMapper.selectByExample(example);
        PageUtil<TbSku> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    public boolean saveTbSku(TbSku tbSku) {
        int insert = tbSkuMapper.insert(tbSku);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbSku(TbSku tbSku) {
        int i = tbSkuMapper.updateByPrimaryKeySelective(tbSku);
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        int i = tbSkuMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbSku findById(String id) {
        TbSku tbSku = tbSkuMapper.selectByPrimaryKey(id);
        return tbSku;
    }

    @Override
    public SpuAndSku finkSkuAndSpuBySkuId(String id) {
        SpuAndSku spuAndSku = tbSkuMapper.finkSkuAndSpuBySkuId(id);
        return spuAndSku;
    }

    @Transactional
    @Override
    public boolean deductionStock(List<TbOrderItem> list) {
        // tbSkuMapper.deductionStock(id, num);
        // 检查是否可以扣减库存
        boolean blo=true;
        for (TbOrderItem item:list){
            TbSku sku = findById(item.getSkuId());
            if(sku==null){
                blo=false;
                break;
            }
            if(!"1".equals(sku.getStatus())){
                blo=false;
                break;
            }
            if(sku.getNum().intValue()<item.getNum().intValue()){
                blo=false;
                break;
            }
        }
        if(blo){
            for (TbOrderItem item:list){
                tbSkuMapper.deductionStock(item.getSkuId(), item.getNum()); //扣减库存
                tbSkuMapper.addSaleNum(item.getSkuId(), item.getNum()); // 增加销量
            }
        }
        return blo;
    }


    @Override
    public List<TbSku> findSkuBySaleNum() {
        Example example = new Example(TbSku.class);
        example.orderBy("saleNum");
        PageHelper.startPage(1,4);
        List<TbSku> tbSkus = tbSkuMapper.selectByExample(example);

        return tbSkus;
    }

    @Override
    public boolean deductionStock2(List<TbOrderItem> list) {

        boolean blo=true;
        for (TbOrderItem item:list){
            TbSku sku = findById(item.getSkuId());
            if(sku==null){
                blo=false;
                break;
            }
            if(!"1".equals(sku.getStatus())){
                blo=false;
                break;
            }
        }
        if(blo){
            for (TbOrderItem item:list){
                tbSkuMapper.deductionStock(item.getSkuId(), -item.getNum());
                tbSkuMapper.addSaleNum(item.getSkuId(), -item.getNum());
            }
        }
        return blo;
    }
}
